//
//  ModifyController.m
//  PropertyMananger
//
//  Created by bh on 17/3/2.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "ModifyController.h"
#import "ModifyPwdCell.h"
#import "ModfiyComfimCell.h"

@interface ModifyController ()

@property (nonatomic,strong) NSArray * titleArray;
@property (nonatomic,strong) NSArray * placeTitleArray;
@property (nonatomic,copy) NSString * oldPassword;
@property (nonatomic,copy) NSString * nowPassword;

@end

@implementation ModifyController
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;

}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initTitleViewName:@"修改密码"];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH - 64) isGroup:NO];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BHGrayColor;
    
    self.titleArray = @[@"当前密码",@"新密码",@"确认密码"];
    self.placeTitleArray = @[@"请输入当前密码",@"请输入新密码",@"请再次输入确认密码"];
    
    
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 3;
    }else{
     return 1;
    }
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        
        ModifyPwdCell * cell = [ModifyPwdCell cellWithTableView:tableView];
        
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.inputTF.placeholder = self.placeTitleArray[indexPath.row];
        if (indexPath.row == 0) {
            
            cell.starImg.image = IMG(@"ico_password_primary");
            
        }else if (indexPath.row == 1){
        
        cell.starImg.image = IMG(@"ico_password_news");
            
        }else if (indexPath.row == 2){
        
        cell.starImg.image = IMG(@"ico_password_news_confirm");
            
        }
        
        
        return cell;
    }else if (indexPath.section == 1){
    
        ModfiyComfimCell * cell = [ModfiyComfimCell cellWithTableView:tableView];
    
        [cell.comfirmBtn addTarget:self action:@selector(comfirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        return cell;
    }
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        
        return 50;
        
    }else{
    
    return 125;
    }
    

}

#pragma mark - 点击提交修改密码
- (void)comfirmBtnClick:(UIButton *)button{

    ModifyPwdCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    self.oldPassword = cell.inputTF.text;
    
    ModifyPwdCell * cell1 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    self.nowPassword = cell1.inputTF.text;
    
    //加密
    self.oldPassword = [BHEncryption encode:cell.inputTF.text key:Key];
    self.nowPassword = [BHEncryption encode:cell1.inputTF.text key:Key];//密码 需要加密
    
    DLog(@"点击修改密码");
    [self postModifyMyPassWorkData];
}

#pragma mark - 数据请求
- (void)postModifyMyPassWorkData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    NSDictionary * parms = nil;
    parms =  @{
               @"WorkerID":userModel.WorkerID,
               @"OldPassword":self.oldPassword,
               @"NewPassword":self.nowPassword,
               @"json":@"\{\"jsonEntity\":\{},\"action\":\"updatepwd\",\"version\":\"V1.0\"}"
               };
    
    DLog(@"提交的参数 === %@",parms);
    
    [MineRequest postModifyMyPassWorkSuccess:^(id data) {
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * personDic = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"返回 === %@",dict[@"Code"]);
        DLog(@"返回提示 === %@",dict[@"Msg"]);
        DLog(@"我的个人信息数据 == %@",personDic);
        if ([[dict[@"Code"]stringValue] isEqualToString:@"1"]) {
            
            [MBProgressHUD showSuccess:@"修改成功" time:1.5];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        if ([[dict[@"Code"]stringValue] isEqualToString:@"-3"]) {
            
            [MBProgressHUD showError:dict[@"Msg"] time:1.5];
            
            [self.navigationController popViewControllerAnimated:YES];
        }

        
        
        
    
        //保存用户密码到沙盒中
       [[NSUserDefaults standardUserDefaults] setObject:self.nowPassword forKey:Password];
        
        [MBProgressHUD hideHUDForView:self.view];
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        [MBProgressHUD hideHUD];
        
    } param:parms ];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}


@end
